package com.wms.web.action;

import com.wms.model.Size;
import com.wms.result.ResultMap;
import com.wms.result.ResultTab;
import com.wms.service.SizeService;
import com.wms.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author This
 * @version 1.0
 * @date 2019/7/24 21:41
 */
public class SizeAction extends BaseAction<Size> {

    @Autowired
    private SizeService service;

    @Override
    public void save() throws IOException {
        if (getModel() == null || StringUtils.isEmpty(getModel().getSizeName())) {
            responseJson(new ResultMap(0, "系统错误，请重试!"), null);
            return;
        }
        if (service.add(getModel())) {
            responseJson(new ResultMap(1, "保存成功!"), null);
            return;
        }
        responseJson(new ResultMap(0, "产品尺寸已存在，请勿重复添加!"), null);
    }

    @Override
    public void edit() {

    }

    @Override
    public void delete() throws IOException {
        if (getModel().getId() != null && getModel().getId() != 0) {
            service.delete(getModel());
        }
        if (StringUtils.isNotEmpty(ids)) {
            service.deleteAll(ids);
        }
        responseJson(new ResultMap(1, "删除成功！"));
    }

    @Override
    public void showList() throws IOException {
        List<Size> sizes = service.findAll();
        if (null != sizes) {
            responseJson(new ResultMap(1, "返回成功", sizes));
        }
    }

    public void pageList() throws Exception {
        if (!CommonUtils.hasPage(page, limit)) {
            responseJson(CommonUtils.failResult());
            return;
        }
        pb.setCurrentPage(page);
        pb.setPageSize(limit);
        String sizeName = getModel().getSizeName();
        if (StringUtils.isNotEmpty(sizeName)) {
            String condition = " size_name like '%" + sizeName + "%' ";
            pb.setCondition(condition);
        }
        service.pageQuery(pb);
        ResultTab resultTab = CommonUtils.responseRst(pb);
        responseJson(resultTab);
    }
}
